const baseURL = process.env.VUE_APP_BASE_API;

// 表单重置
export function resetForm(this: any, refName: string): void {
  if (this.$refs[refName] !== undefined) {
    this.$refs[refName].resetFields();
  }
}

export function isMobile() {
  return navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
}

// 通用下载方法
// export function download(fileName: string): void {
//  let url=   baseURL + "/resources/download?fileName=" + encodeURI(fileName);
//     window.open(url,'_blank')
// }
// 通用下载方法
export function download(fileName) {
  window.location.href = baseURL + '/resources/download?fileName=' + encodeURI(fileName);
}


export const mimeMap = {
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip',
};

/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
export function resolveBlob(res, mimeType) {
  const aLink = document.createElement('a');
  const blob = new Blob([res.data], {type: mimeType});
  // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  const patt = new RegExp('filename=([^;]+\\.[^.;]+);*');
  const contentDisposition = decodeURI(res.headers['content-disposition']);
  const result = patt.exec(contentDisposition);
  if (result) {
    let fileName = result[1];
    fileName = fileName.replace(/"/g, '');
    aLink.href = URL.createObjectURL(blob);
    aLink.setAttribute('download', fileName); // 设置下载文件名称
    document.body.appendChild(aLink);
    aLink.click();
    document.body.appendChild(aLink);
  }
}
